package com.ssy.lingxi.member.merchant.entity;

import com.ssy.lingxi.member.merchant.handler.converter.JpaJsonToMapConverter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 业务平台菜单 - 数据来源于Pass平台的配置
 * @author 万宁
 * @version 2.0.0
 * @date 2020-06-15
 */
@Entity
@Table(schema = "public", name = "ms_mc_menu", indexes = {@Index(name = "ms_mc_menu_paas_id_idx", columnList = "paasId"), @Index(name = "ms_mc_menu_source_idx", columnList = "source")})
public class MenuDO implements Serializable {
    private static final long serialVersionUID = -8786997413242848212L;

    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="menu_seq")
    @SequenceGenerator(sequenceName="ms_mc_menu_seq", name="menu_seq",allocationSize = 1)
    private Long id;

    /**
     * 配置Id
     */
    @Column
    private Long paasId;

    /**
     * 上级菜单Id
     */
    @Column
    private Long parentId;

    /**
     * 菜单标识
     */
    @Column(columnDefinition = "varchar(200)", nullable = false)
    private String key;

    /**
     * 前端定义的菜单编码
     */
    @Column(columnDefinition = "varchar(200)")
    private String code;

    /**
     * 菜单名称
     */
    @Column(columnDefinition = "varchar(50)", nullable = false)
    private String title;

    /**
     * 菜单关联的Url
     */
    @Column(columnDefinition = "varchar(400)")
    private String url;

    /**
     * 同级内排序
     */
    @Column(name="menu_order", columnDefinition = "int4", nullable = false)
    private Integer order;

    /**
     * 前端页面显示菜单权限时，是否要上移一层
     */
    @Column
    private Integer up;

    /**
     * 是否禁用 false-不禁用 true-禁用
     */
    @Column(columnDefinition = "bool")
    private Boolean disabled;

    /**
     * 菜单层级，从1开始
     */
    @Column(columnDefinition = "int4", nullable = false)
    private Integer level;

    /**
     * 菜单描述
     */
    @Column(columnDefinition = "varchar(100)")
    private String remark;

    /**
     * 菜单适用的平台客户端，与UserLoginSourceEnum的定义相同
     */
    @Column(columnDefinition = "int")
    private Integer source;

    /**
     * 是否可配置数据权限，0-否，1-是
     */
    @Column(columnDefinition = "int")
    private Integer dataAuthConfig;

    /**
     * 菜单属性
     */
    @Convert(converter = JpaJsonToMapConverter.class)
    @Column(columnDefinition = "jsonb")
    private Map<String, Object> attrs;

    /**
     * 一对多单向关联的按钮
     */
    @OneToMany(cascade = {CascadeType.DETACH, CascadeType.REMOVE}, fetch = FetchType.LAZY)
    @JoinColumn(name = "menu_id", referencedColumnName="id", foreignKey = @ForeignKey(name = "none",value = ConstraintMode.NO_CONSTRAINT))
    private Set<MenuButtonDO> buttons;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getPaasId() {
        return paasId;
    }

    public void setPaasId(Long paasId) {
        this.paasId = paasId;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    public Integer getUp() {
        return up;
    }

    public void setUp(Integer up) {
        this.up = up;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Integer getSource() {
        return source;
    }

    public void setSource(Integer source) {
        this.source = source;
    }

    public Integer getDataAuthConfig() {
        return dataAuthConfig;
    }

    public void setDataAuthConfig(Integer dataAuthConfig) {
        this.dataAuthConfig = dataAuthConfig;
    }

    public Map<String, Object> getAttrs() {
        return attrs;
    }

    public void setAttrs(Map<String, Object> attrs) {
        this.attrs = attrs;
    }

    public Set<MenuButtonDO> getButtons() {
        return buttons;
    }

    public void setButtons(Set<MenuButtonDO> buttons) {
        this.buttons = buttons;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        MenuDO menuDO = (MenuDO) o;
        return Objects.equals(id, menuDO.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
